package com.springbootcrud.user.controller;

import com.springbootcrud.user.entity.User;
import com.springbootcrud.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Sun on 2020/4/9.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping("/getAll")
    public List<User> getAll(Map map){
        return userService.getAll(map);
    }

    //硬删除
    @RequestMapping("/delById")
    public String delete(Integer id){
        userService.delById(id);
        return "redirect:/index.html";
    }
    @ResponseBody
    @RequestMapping("/save")
    public String save(@RequestBody User user){
       userService.save(user);
       return "success";
    }

    //修改是根据用户的id查询用户
    @RequestMapping("/getUserById")
    @ResponseBody
    public User getUserById(Integer id){
       return   userService.getUserById(id);
    }
    //修改
    @RequestMapping("/updateUserById")
    @ResponseBody
    public String updateUserById(@RequestBody User user){
         userService.updateUserById(user);
         return "success";
    }
}
